﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class dangmuahang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Kết nối tới CSDL
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=localhost;Initial Catalog=chodientu;Integrated Security=True";
        conn.Open();

        string str="";
        if (Session["Cart"] == null)
        {
            message.Text = "Bạn chưa có hàng trong giỏ hàng";
        }
        else
        {
            List<string> list = new List<string>();
            list = (List<string>)Session["Cart"];
            int count = list.Count;
            if (count == 0)
            {
                message.Text = "Bạn chưa có hàng trong giỏ hàng";
            }
           
            else
            {   

                DataSet ds = new DataSet();
                foreach (var item in list)
                {
                    string[] t = item.Split('-');
                    count--;
                    if (count > 0)
                    {
                        str += "masanpham='" + t[0] + "' or ";
                    }
                    else
                        str += "masanpham='" + t[0] + "'";
                }
                string sql = "SELECT distinct Shop.mashop, Shop.Tenshop FROM SANPHAM, SHOP where ( " + str + ") and sanpham.Mashop = Shop.mashop";
                //message.Text = sql;
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                adapter.Fill(ds, "shop");
                foreach (var item in list)
                {
                    string[] t = item.Split('-');
                    string sanpham = t[0];
                    string soluong = t[1];
                    
                    string sql1 = "SELECT *,"+ soluong+" as soluong FROM SANPHAM, SHOP where masanpham ='" + sanpham + "' and sanpham.Mashop = Shop.mashop";
                    SqlDataAdapter adapter2 = new SqlDataAdapter(sql1, conn);
                    adapter2.Fill(ds, "sanpham");
                }
                ds.Relations.Add("myrelation", ds.Tables["shop"].Columns["mashop"], ds.Tables["sanpham"].Columns["mashop"]);
                parent_repeater.DataSource = ds.Tables["shop"];
                Page.DataBind();
                conn.Close();
                
            }

        }
        if (Request["remove"] != null)
        {
            string str_remove = Request["remove"];
            List<string> list_remove = new List<string>();
            list_remove = (List<string>)Session["Cart"];
            foreach (var item in list_remove )
            {
                string[] t = item.Split('-');
                if (t[0] == str_remove)
                {
                    list_remove.Remove(item);
                    break;
                }

            }
            Response.Redirect("Giohang.aspx");
        }
       
    }
    
}